<template>
    <div>
      <!--  查询  -->
      <el-card class="dis">
        <el-form :inline="true" :model="formInline" class="demo-form-inline cardH">
          <el-form-item label="参数名称">
            <el-input v-model="formInline.name" size="mini" clearable placeholder="请输入等级名称" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" size="mini" @click="search">查询</el-button>
            <el-button type="primary" size="mini" @click="addOrEdit(0)">新增</el-button>
          </el-form-item>
        </el-form>
      </el-card>
      <!-- 列表 -->
      <el-card style="margin-top: 20px">
        <!--  table  -->
        <el-table
          :data="tableData"
          style="width: 100%;margin-top: 20px"
          size="mini"
        >
          <el-table-column
            prop="id"
            label="等级id"
            align="center"
            width="200"
          />
          <el-table-column
            prop="name"
            label="级别名称"
            align="center"
            width="300"
          />
          <el-table-column
            prop="describe"
            label="报警描述"
            align="center"
          />
          <el-table-column
            fixed="right"
            label="操作"
            width="400"
            align="center"
          >
            <template slot-scope="scope">
              <el-button
                type="info"
                size="mini"
                @click="info(scope.row)"
              >
                详情
              </el-button>
              <el-button
                type="warning"
                size="mini"
                @click="addOrEdit(scope.row.id)"
              >
                编辑
              </el-button>
              <el-button
                type="danger"
                size="mini"
                @click="del(scope.row.id)"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <!--   分页   -->
<!--        <el-pagination-->
<!--          style="float: right;"-->
<!--          :page-size="10"-->
<!--          :current-page="formInline.offset"-->
<!--          layout="total, prev, pager, next, jumper"-->
<!--          :total="total"-->
<!--          @current-change="handleCurrentChange"-->
<!--        />-->
        <pagination style='float: right;' v-show='total>0' :total='total' :page.sync='formInline.offset' :limit.sync='formInline.limit' @pagination='getList' />
      </el-card>
      <Detail
        ref="warningLevelDetail"
        :id="id"
        @reload="reload"
      />
      <warning-level-info ref='WarningLevelInfo'></warning-level-info>
    </div>
</template>

<script>
  import { warningList, deteleWarningLevel } from '@/api/warning'
  import Detail from './components/detail'
  import WarningLevelInfo from '@/views/warningManage/warningLevel/components/info';
  export default {
    name: "index",
    components: {
      WarningLevelInfo,
      Detail
    },
    data() {
      return{
        id: 0, // 编辑id
        total: 0, // 数据总量
        tableData: [], // 列表数据
        // 列表搜索数据
        formInline: {
          sort: 'created_at',
          offset: 1,
          limit: 10,
          order: 'descending',
          name: ''
        }
      }
    },
    created() {
      this.getList()
    },
    methods: {
      /**
       * @description: 删除数据
       * @author: hst
       */
      del(id) {
        this.$confirm('是否要删除此条数据?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(res => {
          deteleWarningLevel({id}).then(res1 => {
            this.$message({
              type: 'success',
              message: '删除成功'
            })
            this.getList()
          })
        })
      },
      /**
       * @description: 重置列表
       * @author: hst
       */
      reload() {
        this.formInline.offset = 1
        this.formInline.name = ''
        this.getList()
      },
      /**
       * @description: 获取列表数据
       * @author: hst
       */
      getList() {
        warningList(this.formInline).then(res => {
          this.tableData = res.data.list
          this.total = res.data.total
        })
      },
      // 指定页码数据
      handleCurrentChange(val) {
        this.formInline.offset = val
        this.getList()
      },
      /**
       * @description: 查询
       * @author: 书童
       */
      search() {
        this.formInline.offset = 0
        this.getList()
      },
      /**
       * @description: 新增
       * @author: 书童
       */
      addOrEdit(id) {
        this.id = id
        this.$refs.warningLevelDetail.open()
      },
      info(row){
        this.$refs.WarningLevelInfo.open(row);
      }
    }
  }
</script>

<style lang="scss" scoped>
  .cardH{
    height: 38px;
  }
</style>
